﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace BuildHelper.Work.GUI
{
    public partial class DevEnvChooser : Form
    {
        public static List<DevEnvProc> GetDevEnvs(GuiDataSet ds, string stm, bool addGcm)
        {
            List<DevEnvProc> list = new List<DevEnvProc>();
            var devenvs = GlobalTools.FilesTools.GetDevEnvs();

            GetDevEnvs(devenvs, list, ds, stm);
            if (addGcm)
            {
                DataRow[] rows = ds.APPS.Select("EXE is NULL OR EXE = ''");
                foreach (DataRow r in rows)
                {
                    if (r["STM"].AsString() != stm)
                        GetDevEnvs(devenvs, list, ds, r["STM"].AsString());
                }
            }
            return list;
        }

        private static void GetDevEnvs(List<BuildHelper.GlobalTools.FilesTools.DevEnvInfo> devenvs, List<DevEnvProc> list, GuiDataSet ds, string stm)
        {
            DataRow slnRow = ds.APPS.SelectFirstRow("STM = " + stm.EscapeSql());
            if (slnRow != null)
            {
                string sln = slnRow["SLN"].AsString();
                Process p = GlobalTools.FilesTools.GetDevEnv(sln, devenvs);
                if (p != null)
                {
                    list.Add(new DevEnvProc(p, stm));
                }
                try
                {
                    string[] otherSln = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(sln));
                    if (otherSln != null)
                    {
                        foreach (string s in otherSln)
                        {
                            if (s.ToUpper() != sln.ToUpper())
                            {
                                p = GlobalTools.FilesTools.GetDevEnv(s, devenvs);
                                if (p != null)
                                {
                                    list.Add(new DevEnvProc(p, stm + " (" + System.IO.Path.GetFileNameWithoutExtension(s) + ")"));
                                }
                            }
                        }
                    }
                }
                catch { }
            }
            DataRow[] rows = ds.PROJEKTY.Select("STM = " + stm.EscapeSql());
            foreach (DataRow r in rows)
            {
                Process p = GlobalTools.FilesTools.GetDevEnv(r["CSPROJ"].AsString(), devenvs);
                if (p != null)
                {
                    list.Add(new DevEnvProc(p, r["NAZWA"].AsString()));
                }
            }
        }
        public DevEnvChooser()
        {
            InitializeComponent();
        }

        public DevEnvChooser(List<DevEnvProc> devenvs)
        {
            InitializeComponent();
            this.gcMain.DataSource = new BindingList<DevEnvProc>(devenvs);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DevEnvProc p = gridView1.GetRow(gridView1.FocusedRowHandle) as DevEnvProc;
            if (p != null)
            {
                SelectedDevEnv = p.DevEnv;
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        public Process SelectedDevEnv { get; private set; }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            simpleButton1.PerformClick();
        }

    }

    public class DevEnvProc
    {
        public DevEnvProc(Process p, string nazwa)
        {
            DevEnv = p;
            Projekt = nazwa;
        }
        public Process DevEnv { get; set; }
        public string Projekt { get; set; }
    }
}
